<?php
/*
* mod_jxmarks is just a simple module that loads shared bookmarks from Xmarks and displays it in Joomla.
* @copyright (c) Copyright: Cecilomar Design.
* @author info@cecilomar.com 
* @date 2009.07.23
* @package Joomla1.5

*/

// no direct access
defined('_JEXEC') or die('Restricted access');

/////////////////////////////////////////////////////////////////////////////////////////////////////
//  GET MODULE PARAMETERS  //////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

// The Module Class Suffix
$jmx['class']		= $params->get( 'jxmclass' );
// Cache the RSS file and module true or false
$jmx['cache']		= $params->get( 'cache' );
// Time in minutes for the RSS to be cached
$jmx['cachetime']	= $params->get( 'cachetime' );
// URL of the RSS from Xmarks
$jmx['file']		= $params->get( 'jxmfile' );
// Target of the link
$jxm['target']		= $params->get( 'target' );
// Attributes to be added to the link
$jxm['attributes']	= $params->get( 'attributes' );


/////////////////////////////////////////////////////////////////////////////////////////////////////
//  CACHE  //////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

// Explode the URL by "/"
$jmx['exurl'] = explode("/", $jmx['file']);

// Validate the URL
if(
	$jmx['exurl'][0] == 'http:' and
	$jmx['exurl'][1] == '' and
	$jmx['exurl'][2] == 'share.xmarks.com' and
	$jmx['exurl'][3] == 'folder' and
	$jmx['exurl'][4] == 'rss'
){

// Take the last array to create the ID for the XML cache file to be saved
$jmx['cacheid'] = end($jmx['exurl']);
$jmx['cachefile'] = dirname(__FILE__).DS.'cache/'.$jmx['cacheid'].'.xml';

// Find out if the RSS has been cached and/or how much time ago
$jmx['cachetime'] = $jmx['cachetime'] * 60; // Minutes * 60sec.
// Serve from the cache if it is younger than $jmx['cachetime']
if (file_exists($jmx['cachefile']) && (time() - $jmx['cachetime'] < filemtime($jmx['cachefile']))) {
} else { // If there is no chache saved or is older than the cache time create a new cache
	// open the cache file for writing
	$fp = fopen($jmx['cachefile'], 'w');
	// save the contents of output buffer to the file
	fwrite($fp, file_get_contents($jmx['file']));
	// close the file
	fclose($fp);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//  LOAD THE CACHE AND PROCESS THE RSS  /////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

if($jmx['file'] == false){
	$jmx['file'] = $jmx['cachefile'];
}

// require the XML parser
require_once( dirname(__FILE__).DS.'sofeexmlparser.php' );
$jxmxml = new SofeeXmlParser(); 
$jxmxml->parseFile($jmx['file']); 
$jxmarks = $jxmxml->getTree(); 

// Check if the RSS is good or if is unable to load the bookmarks.
if(isset($jxmarks['rss']['channel']['item'])){
$jxmarks = $jxmarks['rss']['channel']['item'];

/////////////////////////////////////////////////////////////////////////////////////////////////////
//  DISPLAY THE PROCESSED DATA  /////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

//	1. A <div> to identify for easy CSS access
//	2. Starts the <ul> list
//	3. A foreach loop
//	4. There go the <li>
//	5. <a> link with target and attributes
//	6. Prints the description, if there is any
//	7. close </li>
//	8. repeat loop...
//	9. close <div>
?>
<div class="jxmarks<?php if($jxmarks){ echo $jmx['class']; } ?>">
    <ul>
<?php
    
    foreach ($jxmarks as $jxmkey => $jxmvalue) {
?>
        <li>
            <div class="jxm_title"><a target="<?php echo $jxmvalue['link']['value']; ?>" <?php echo $jxm['attributes'];?> href="<?php echo $jxmvalue['link']['value']; ?>"><?php echo $jxmvalue['title']['value']; ?></a><div>
<?php if ($jxmvalue['description']['value']){ ?>
            <div class="jxm_description"><?php echo $jxmvalue['description']['value']; ?></div>
<?php } ?>
        </li>
<?php
    }
    
    
?>
    </ul>
</div>
<?php } else{
// If the RSS URL is not valid, doesn't prints any viewable message.
?>
<!-- JXmarks was unable to load bookmarks. Bookmark not shared or bad ID -->
<?php
}
} else { ?>
<!-- JXmarks was unable to load bookmarks. Bad URL -->
<?php }
// Unset Useless Variables to finish whithout worries...
unset($jxmarks);

?>